{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Want to Publish Stock Alerts to Slack?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before getting started please confirm the [the notebook-integration docker stack is running](https://github.com/AlgoTraders/stock-analysis-engine#jupyter)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Publishing FinViz Screeners to Slack"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Please edit your ```./compose/envs/jupyter.env``` file and add the following Slack webhook to your docker environment variables:\n",
    "    \n",
    "```\n",
    "SLACK_WEBHOOK=https://hooks.slack.com/services/<your remaining webhook path>\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Restart the Stack"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Please restart the stack so the new ``SLACK_WEBHOOK`` environment variable can take effect. If you are using docker compose please restart the stack with the commands:\n",
    "\n",
    "```\n",
    "$ ./compose/stop.sh -j\n",
    "$ ./compose/start.sh -j\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pick a FinViz Screener\n",
    "\n",
    "This notebook uses [a simple NYSE Dow Jones Index Financials with a P/E above 5 screener url](https://finviz.com/screener.ashx?v=111&f=exch_nyse,fa_pe_o5,idx_dji,sec_financial&ft=4) to fetch a list tickers and a ``pandas.DataFrame`` from the resulting html table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# After you build a FinViz screen just paste the url here:\n",
    "url = 'https://finviz.com/screener.ashx?v=111&f=exch_nyse,fa_pe_o5,idx_dji,sec_financial&ft=4'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "import pandas as pd\n",
    "from IPython.display import display\n",
    "from IPython.display import HTML\n",
    "from analysis_engine.consts import SUCCESS\n",
    "from analysis_engine.consts import SLACK_FINVIZ_COLUMNS\n",
    "from analysis_engine.consts import get_status\n",
    "from analysis_engine.consts import ev\n",
    "from analysis_engine.finviz.fetch_api import fetch_tickers_from_screener\n",
    "from analysis_engine.send_to_slack import post_df\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Fetch Tickers from FinViz Screener"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fetching tickers from url: https://finviz.com/screener.ashx?v=111&f=exch_nyse,fa_pe_o5,idx_dji,sec_financial&ft=4\n",
      "\n",
      "2018-10-17 23:51:08,371 - analysis_engine.finviz.fetch_api - INFO - fz-screen-converter fetching url=https://finviz.com/screener.ashx?v=111&f=exch_nyse,fa_pe_o5,idx_dji,sec_financial&ft=4\n",
      "2018-10-17 23:51:09,963 - analysis_engine.finviz.fetch_api - INFO - fz-screen-converter fetch done - df=5 from url=https://finviz.com/screener.ashx?v=111&f=exch_nyse,fa_pe_o5,idx_dji,sec_financial&ft=4 with tickers=['AXP', 'GS', 'JPM', 'TRV', 'V'] rows=5\n",
      "Found tickers: ['AXP', 'GS', 'JPM', 'TRV', 'V']\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>change</th>\n",
       "      <th>company</th>\n",
       "      <th>country</th>\n",
       "      <th>industry</th>\n",
       "      <th>market_cap</th>\n",
       "      <th>pe</th>\n",
       "      <th>price</th>\n",
       "      <th>sector</th>\n",
       "      <th>ticker</th>\n",
       "      <th>ticker_id</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.07%</td>\n",
       "      <td>American Express Company</td>\n",
       "      <td>USA</td>\n",
       "      <td>Credit Services</td>\n",
       "      <td>89.73B</td>\n",
       "      <td>15.30</td>\n",
       "      <td>104.34</td>\n",
       "      <td>Financial</td>\n",
       "      <td>AXP</td>\n",
       "      <td>1</td>\n",
       "      <td>3,789,094</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.97%</td>\n",
       "      <td>The Goldman Sachs Group, Inc.</td>\n",
       "      <td>USA</td>\n",
       "      <td>Investment Brokerage - National</td>\n",
       "      <td>85.83B</td>\n",
       "      <td>9.61</td>\n",
       "      <td>228.28</td>\n",
       "      <td>Financial</td>\n",
       "      <td>GS</td>\n",
       "      <td>2</td>\n",
       "      <td>4,951,867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.11%</td>\n",
       "      <td>JPMorgan Chase &amp; Co.</td>\n",
       "      <td>USA</td>\n",
       "      <td>Money Center Banks</td>\n",
       "      <td>373.20B</td>\n",
       "      <td>13.67</td>\n",
       "      <td>109.83</td>\n",
       "      <td>Financial</td>\n",
       "      <td>JPM</td>\n",
       "      <td>3</td>\n",
       "      <td>18,650,929</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.35%</td>\n",
       "      <td>The Travelers Companies, Inc.</td>\n",
       "      <td>USA</td>\n",
       "      <td>Property &amp; Casualty Insurance</td>\n",
       "      <td>34.03B</td>\n",
       "      <td>16.11</td>\n",
       "      <td>126.44</td>\n",
       "      <td>Financial</td>\n",
       "      <td>TRV</td>\n",
       "      <td>4</td>\n",
       "      <td>2,106,779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.50%</td>\n",
       "      <td>Visa Inc.</td>\n",
       "      <td>USA</td>\n",
       "      <td>Credit Services</td>\n",
       "      <td>302.64B</td>\n",
       "      <td>39.29</td>\n",
       "      <td>142.45</td>\n",
       "      <td>Financial</td>\n",
       "      <td>V</td>\n",
       "      <td>5</td>\n",
       "      <td>9,167,145</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  change                        company country  \\\n",
       "0  0.07%       American Express Company     USA   \n",
       "1  2.97%  The Goldman Sachs Group, Inc.     USA   \n",
       "2  1.11%           JPMorgan Chase & Co.     USA   \n",
       "3  1.35%  The Travelers Companies, Inc.     USA   \n",
       "4  0.50%                      Visa Inc.     USA   \n",
       "\n",
       "                          industry market_cap     pe   price     sector  \\\n",
       "0                  Credit Services     89.73B  15.30  104.34  Financial   \n",
       "1  Investment Brokerage - National     85.83B   9.61  228.28  Financial   \n",
       "2               Money Center Banks    373.20B  13.67  109.83  Financial   \n",
       "3    Property & Casualty Insurance     34.03B  16.11  126.44  Financial   \n",
       "4                  Credit Services    302.64B  39.29  142.45  Financial   \n",
       "\n",
       "  ticker ticker_id      volume  \n",
       "0    AXP         1   3,789,094  \n",
       "1     GS         2   4,951,867  \n",
       "2    JPM         3  18,650,929  \n",
       "3    TRV         4   2,106,779  \n",
       "4      V         5   9,167,145  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'Fetching tickers from url: {url}\\n')\n",
    "res = fetch_tickers_from_screener(url=url)\n",
    "\n",
    "df = None\n",
    "\n",
    "if res['status'] != SUCCESS:\n",
    "    print(f'Failed to fetch tickers from FinViz url={url} with error={res["err"]} and status={get_status(status=res["status"])}')\n",
    "else:\n",
    "    print(f'Found tickers: {res["rec"]["tickers"]}')\n",
    "    df = res['rec']['data']\n",
    "    display(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Publish the FinViz HTML Table to Slack\n",
    "\n",
    "With your ``SLACK_WEBHOOK`` environment variable in place, Jupyter should be able to publish the FinViz's screener table to Slack."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-10-17 23:51:10,312 - analysis_engine.send_to_slack - INFO - Successful post of attachment=True to slack_webhook\n",
      "2018-10-17 23:51:10,551 - analysis_engine.send_to_slack - INFO - Successful post of attachment=True to slack_webhook\n"
     ]
    }
   ],
   "source": [
    "if ev('SLACK_WEBHOOK', False):\n",
    "    # Post the pandas.DataFrame as a Jupyter code block message to Slack\n",
    "    post_df(\n",
    "        df=df[SLACK_FINVIZ_COLUMNS],\n",
    "        columns=SLACK_FINVIZ_COLUMNS)\n",
    "else:\n",
    "    print('Missing the SLACK_WEBHOOK environment variable\\nUnable to publish to Slack without this environment variable')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
